package com.jiuyou.clientController.collect;

import com.alibaba.fastjson.JSONObject;
import com.jiuyou.clientController.base.ClientBaseController;
import com.ruoyi.collect.domain.Collect;
import com.ruoyi.collect.service.ICollectService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.MemberTokenService;
import com.ruoyi.product.domain.StoreProduct;
import com.ruoyi.product.service.IStoreProductService;
import com.ruoyi.shopping.domain.MallProduct;
import com.ruoyi.shopping.service.IMallProductService;
import com.ruoyi.store.domain.Store;
import com.ruoyi.store.service.IStoreService;
import com.ruoyi.store.service.impl.StoreServiceImpl;
import com.ruoyi.storeclassify.domain.Storeclassify;
import com.ruoyi.storeclassify.service.IStoreclassifyService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

/**
 * 收藏
 *
 * @author jiuyou
 */
@Api(tags = {"C端收藏相关"})
@RestController
@RequestMapping("/client/collect")
public class CollectClientController extends ClientBaseController
{
    private static final Logger log = LoggerFactory.getLogger(CollectClientController.class);

    @Autowired
    private ICollectService collectService;
    @Autowired
    private IStoreService storeService;
    @Autowired
    private IStoreclassifyService storeclassifyService;
    @Autowired
    private IStoreProductService storeProductService;
    @Autowired
    private IMallProductService mallProductService;

    /**
     * 查询收藏记录列表
     */
    @ApiOperation(value = "查询收藏记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前第几页",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize",value = "每页几条数据",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "collectType",value = "收藏类型，门店传store,商品传product",required = true,dataType = "String"),
    })
    @PostMapping("/list")
    public AjaxResult list(String collectType,Integer pageNum,Integer pageSize)
    {
        Long loginMemberId=getLoginMemberId();
        if(null==loginMemberId){
            return AjaxResult.error("登录状态异常");
        }
        Collect collect=new Collect();
        collect.setCollectType(collectType);
        collect.setMemberId(loginMemberId);
        startPage();
        List<Collect> list = collectService.selectCollectList(collect);
        TableDataInfo tableDataInfo =getDataTable(list);
        list= (List<Collect>) tableDataInfo.getRows();
        List<JSONObject> resultList=new ArrayList<JSONObject>();
        for (Collect co:list) {
            JSONObject jso=new JSONObject();
            jso.put("collectType",co.getCollectType());
           if(DicConstants.COLLECT_TYPE_STORE.getCode().equals(co.getCollectType() ))  {
               //门店
               Long storeId=co.getBusId();
               Store store= storeService.selectStoreById(storeId);
               if(null==store){
                   jso=null;
                   continue;
               }
               jso.put("storeImage",store.getStoreImage());
               jso.put("storeName",store.getStoreName());
               jso.put("consumption",store.getConsumption());
               jso.put("storeLevel",store.getStoreLevel());
               Storeclassify storeclassify= storeclassifyService.selectStoreclassifyById(store.getStoreClassify());
               if(null==storeclassify){
                   jso=null;
                   continue;
               }
               jso.put("storeClassify", storeclassify.getClassifyName());
           }
            if(DicConstants.COLLECT_TYPE_PRODUCT.getCode().equals(co.getCollectType() ))  {
                //商品
                Long productId=co.getBusId();
                StoreProduct storeProduct= storeProductService.selectStoreProductById(productId);
                if(null==storeProduct){
                    jso=null;
                    continue;
                }
                jso.put("productName", storeProduct.getProductName());
                jso.put("productImage",storeProduct.getProductImage());
                jso.put("price",storeProduct.getPrice());
                jso.put("storePrice",storeProduct.getStorePrice());
                jso.put("storeName",storeProduct.getStoreName());
                jso.put("busId",storeProduct.getId());
                jso.put("id",co.getId());
            }
            if(DicConstants.COLLECT_TYPE_MALL_PRODUCT.getCode().equals(co.getCollectType() ))  {
                //商城商品
                Long productId=co.getBusId();
                MallProduct product = mallProductService.selectMallProductById(productId);
                if(null==product){
                    jso=null;
                    continue;
                }
                jso.put("productName", product.getProductName());
                jso.put("productImage",product.getProductImage());
                jso.put("price",product.getCurrentPrice());
                jso.put("busId",product.getId());
                jso.put("id",co.getId());
            }
            if(null!=jso){
                resultList.add(jso);
            }
        }
        tableDataInfo.setRows(resultList);
        return AjaxResult.success(tableDataInfo);
    }


    /**
     * 详情页收藏数量
     * */
    @PostMapping("/getCollectNnum")
    public AjaxResult getCollectNnum(@RequestBody Collect collect)
    {
        Long loginMemberId=getLoginMemberId();
        if(null==loginMemberId){
            return AjaxResult.error("登录状态异常");
        }
        //collect.setMemberId(loginMemberId);
        //collect.setCollectType("product");
        List<Collect> list = collectService.selectCollectList(collect);
        return AjaxResult.success(list.size());
    }


    /**
     * 新增收藏记录
     */
    @ApiOperation(value = "添加收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "busId",value = "根据收藏类型不同传门店id或者商品id",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "collectType",value = "收藏类型，门店传store,商品传product",required = true,dataType = "String"),
    })
    @PostMapping("/add")
    public AjaxResult add(String collectType,Long busId,String classifyId)
    {  Long loginMemberId=getLoginMemberId();
        if(null==loginMemberId){
            return AjaxResult.error("登录状态异常");
        }
        Collect collect=new Collect();
        collect.setCollectType(collectType);
        collect.setBusId(busId);
        collect.setMemberId(loginMemberId);
        collect.setClassifyId(classifyId);
        collectService.insertCollect(collect);
        return AjaxResult.success(collect);
    }
    @PostMapping("/query/{busId}/{type}")
    public AjaxResult query(@PathVariable Long busId,@PathVariable String type)
    {  Long loginMemberId=getLoginMemberId();
        if(null==loginMemberId){
            return AjaxResult.error("登录状态异常");
        }
        Collect collect = collectService.selectCollectByOthers(loginMemberId,busId,type);

        return AjaxResult.success(collect);
    }




    /**
     * 删除收藏记录
     */
    @ApiOperation(value = "删除收藏记录，支持数组id删除")
    @ApiImplicitParam(name = "ids",value = "要删除的收藏记录id多个使用英文逗号分割，例如1,2,3,4",required = true)
    @PostMapping("/del/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        Long loginMemberId=getLoginMemberId();
        if(null==loginMemberId){
            return AjaxResult.error("登录状态异常");
        }
        return toAjax(collectService.deleteCollectByIds(ids,loginMemberId));
    }


}
